//
//  VerifyFormatUitil.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/27.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "HTCFVerifyFormatUitil.h"

@implementation HTCFVerifyFormatUitil

#define EMAILFROART @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"

+ (BOOL)checkPhoneNumber:(NSString *)numberStr
{
//    NSPredicate * phone = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^1[3-9]\\d{9}"];//这个是比较笼统的匹配方式
//    NSPredicate * phone = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}"];//这个是最新的手机号码匹配方式
        NSPredicate * phone = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^0?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}"];//这个是最新的手机号码匹配方式

    //    NSPredicate * phone = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$"];
    return [phone evaluateWithObject:numberStr];
}


+ (BOOL)checkPassword:(NSString *)password
{
    NSPredicate * pwd = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[a-zA-Z0-9( _!@#~$%^&)]{6,16}$"];
    return [pwd evaluateWithObject:password];
}

+ (BOOL)checkVerifyCode:(NSString *)verfyCode
{
    NSPredicate * verify = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"\\d{4,6}"];
    return [verify evaluateWithObject:verfyCode];
}

+ (BOOL)checkNickName:(NSString *)nickName
{
    NSPredicate *nickNameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[\\u4E00-\\u9FFF0-9a-zA-Z_\\-]{1,16}"];
    return [nickNameTest evaluateWithObject:nickName];
}

+ (BOOL)checkQQNum:(NSString *)qqNum
{
    NSPredicate *QQ = [NSPredicate predicateWithFormat:@"SELE MATCHES %@",@"[1-9][0-9]{4,}"];
    return [QQ evaluateWithObject:qqNum];
}

+ (BOOL)checkEmailFormarwithEmail:(NSString *)email
{
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"];
    
    return [emailTest evaluateWithObject:email];
}

+ (BOOL)checkIdentifyCardFormat:(NSString *)identifyCard
{
    identifyCard = [identifyCard stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([identifyCard length] != 18)
    {
        return NO;
    }
    NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))";
    NSString *leapMmdd = @"0229";
    NSString *year = @"(19|20)[0-9]{2}";
    NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])";
    NSString *yearMmdd = [NSString stringWithFormat:@"%@%@", year, mmdd];
    NSString *leapyearMmdd = [NSString stringWithFormat:@"%@%@", leapYear, leapMmdd];
    NSString *yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd, @"20000229"];
    NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}";
    NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd , @"[0-9]{3}[0-9Xx]"];
    
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    
    if (![regexTest evaluateWithObject:identifyCard]){
        return NO;
    }
    int summary = ([identifyCard substringWithRange:NSMakeRange(0,1)].intValue + [identifyCard substringWithRange:NSMakeRange(10,1)].intValue) *7
    + ([identifyCard substringWithRange:NSMakeRange(1,1)].intValue + [identifyCard substringWithRange:NSMakeRange(11,1)].intValue) *9
    + ([identifyCard substringWithRange:NSMakeRange(2,1)].intValue + [identifyCard substringWithRange:NSMakeRange(12,1)].intValue) *10
    + ([identifyCard substringWithRange:NSMakeRange(3,1)].intValue + [identifyCard substringWithRange:NSMakeRange(13,1)].intValue) *5
    + ([identifyCard substringWithRange:NSMakeRange(4,1)].intValue + [identifyCard substringWithRange:NSMakeRange(14,1)].intValue) *8
    + ([identifyCard substringWithRange:NSMakeRange(5,1)].intValue + [identifyCard substringWithRange:NSMakeRange(15,1)].intValue) *4
    + ([identifyCard substringWithRange:NSMakeRange(6,1)].intValue + [identifyCard substringWithRange:NSMakeRange(16,1)].intValue) *2
    + [identifyCard substringWithRange:NSMakeRange(7,1)].intValue *1 + [identifyCard substringWithRange:NSMakeRange(8,1)].intValue *6
    + [identifyCard substringWithRange:NSMakeRange(9,1)].intValue *3;
    
    NSInteger remainder   = summary % 11;
    NSString *checkBit    = @"";
    NSString *checkString = @"10X98765432";
    checkBit              = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位
    
    return [checkBit isEqualToString:[[identifyCard substringWithRange:NSMakeRange(17,1)] uppercaseString]];
}

@end
